package cn.bytecollege.user.controller;

import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.OrderDetail;
import cn.bytecollege.common.entity.Product;
import cn.bytecollege.user.service.OrderDetailService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "OrderDetailAddUpdateController",value = "/user/orderdetailaddupdate")
public class OrderDetailAddUpdateController extends HttpServlet {
    OrderDetailService orderDetailService=null;
    ProductService productService=null;
    public OrderDetailAddUpdateController(){
        orderDetailService=new OrderDetailService();
        productService=new ProductService();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String update=request.getParameter("update");
        String orderDetailId=request.getParameter("orderdetailid");
        OrderDetail orderDetail=orderDetailService.findById(Integer.valueOf(orderDetailId));
        if(update.equals("0")){           //0为增加,1为减少
            Integer ProductNumber=orderDetail.getProductNumber();
            ProductNumber-=1;
            orderDetail.setProductNumber(ProductNumber);
            orderDetailService.update(orderDetail);
        } else if(update.equals("1")){
            if(orderDetail.getProductNumber()==null){
                orderDetail.setProductNumber(2);

            }else{
                Integer ProductNumber=orderDetail.getProductNumber();
                ProductNumber+=1;
                orderDetail.setProductNumber(ProductNumber);
            }
            orderDetailService.update(orderDetail);
        }
        String id = request.getParameter("productid");
        Integer productId = Integer.parseInt(id);
        Product product = productService.findClassifyById(productId);
        request.setAttribute("product",product);
        List<Product> lists=productService.findAll();
        request.setAttribute("lists",lists);
        List<Product> linelists=null;
        if(lists.size()>3){
            linelists=lists.subList(0,3);
        }else {
            linelists=lists.subList(0,lists.size());
        }
        request.setAttribute("linelists",linelists);
        request.setAttribute("orderDetail",orderDetail);
        request.getRequestDispatcher("/productview.jsp").forward(request,response);




    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request, response);
    }
}
